package com.decoration.basic.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

 public class DateFormatUtils {

        /**
         * 全格式
         */
        private static final SimpleDateFormat allFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        /**
         * 日期格式
         */
        private static final SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy-MM-dd");

        private static final SimpleDateFormat ymdFmt = new SimpleDateFormat("yyyy/MM/dd");
        /**
         * 时间格式
         */
        private static final SimpleDateFormat timeFmt = new SimpleDateFormat("HH:mm:ss");

        private static final SimpleDateFormat dotDateFmt = new SimpleDateFormat("yyyy.MM.dd");

        public static String format (Date date,String frommat){

            SimpleDateFormat formatter = new SimpleDateFormat(frommat);
            String datestr = formatter.format(date);
            return datestr;
        }
        /**
         * 全格式时间转字符串
         * @return 字符串时间
         */
        public synchronized static String getYmdHmsString(Date date){
            if (date == null){
                return "";
            }
            return allFmt.format(date);
        }

        /**
         * 全格式字符串转时间
         * @param date 字符串时间
         * @return 时间
         */
        public synchronized static Date getYmdHmsDate(String date){
            try {
                return allFmt.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
        }
        /**
         * 全格式时间转字符串
         * @return 字符串时间
         */
        public synchronized static String getYmdString(Date date){
            if (date == null){
                return "";
            }
            return dateFmt.format(date);
        }

        /**
         * 全格式字符串转时间
         * @param date 字符串时间
         * @return 时间
         */
        public synchronized static Date getYmdDate(String date){
            try {
                return dateFmt.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
        }
        /**
         * 时间转时间格式字符串
         * @return 字符串时间
         */
        public synchronized static String getHmsString(Date date){
            if (date == null){
                return "";
            }
            return timeFmt.format(date);
        }

        /**
         * 时间格式字符串转时间
         * @param date 字符串时间
         * @return 时间
         */
        public synchronized static Date getHmsDate(String date){
            try {
                return timeFmt.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
        }

        /**
         * 自定义时间转字符串
         * @param fmt 时间格式
         * @param date 时间
         * @return 指定格式时间字符串
         */
        public synchronized static String diyDateFormat(String fmt,Date date){
            if (date == null){
                return "";
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(fmt);
            return simpleDateFormat.format(date);
        }

        /**
         * 自定义字符串转换时间
         * @param fmt 时间格式
         * @param date 字符串时间
         * @return 指定格式时间
         */
        public synchronized static Date diyStringFormat(String fmt,String date){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(fmt);
            try {
                return simpleDateFormat.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
        }

        /**
         * 获取本月开始时间
         * @return 本月开始时间
         */
        public synchronized static Date getMonthStart(){
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY,0);
            calendar.set(Calendar.SECOND,0);
            calendar.set(Calendar.MINUTE,0);
            calendar.set(Calendar.DAY_OF_MONTH,1);
            return calendar.getTime();
        }
        public synchronized static Date getMonthEnd(){
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH,1);
            calendar.set(Calendar.HOUR_OF_DAY,0);
            calendar.set(Calendar.SECOND,0);
            calendar.set(Calendar.MINUTE,0);
            calendar.set(Calendar.MILLISECOND,0);
            calendar.add(Calendar.MONTH,+1);
            calendar.add(Calendar.MILLISECOND,-1);
            return calendar.getTime();
        }

        /**
         * 获取本日开始时间
         * @return
         */
        public synchronized static Date getDayStart(){
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY,0);
            calendar.set(Calendar.SECOND,0);
            calendar.set(Calendar.MINUTE,0);
            return calendar.getTime();
        }

     /**
      *  获取本日日终时间
      * @return
      */
     public synchronized static Date getDayEnd(){
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY,0);
            calendar.set(Calendar.SECOND,0);
            calendar.set(Calendar.MINUTE,0);
            calendar.add(Calendar.DAY_OF_MONTH,+1);
            calendar.add(Calendar.SECOND,-1);
            return calendar.getTime();
        }

        public synchronized static String getYmdFmtDate(Date date){
             if (date == null){
                 return "";
             }
             return ymdFmt.format(date);
        }
        public synchronized static String getDotYmdFmtDate(Date date){
            if (date == null){
                return "";
            }
            return dotDateFmt.format(date);
        }

         public static String getNowDay(String pattern){
             return getDateStr(new Date(),pattern);
         }

         public static String getDateStr(Date date,String pattern){
             if (date == null){
                 date = new Date();
             }
             SimpleDateFormat formatter = new SimpleDateFormat(pattern);
             String dateStr = formatter.format(date);
             return dateStr;
         }

     public static String convStrToStr(String date,String pattern,String newpattern){
         Date tmpDate = null;
         if (StringUtils.isBlank(date)){
             tmpDate = new Date();
         }else{
             SimpleDateFormat formatter = new SimpleDateFormat(pattern);
             try {
                 tmpDate = formatter.parse(date);
             } catch (ParseException e) {
                 e.printStackTrace();
             }
         }
         if (tmpDate == null){
             return date;
         }
         SimpleDateFormat newformatter = new SimpleDateFormat(newpattern);
         String dateStr = newformatter.format(tmpDate);
         return dateStr;
     }

        public static void main(String []argvs){
            /*System.out.println(getDayStart());
            System.out.println(new Date());*/
            String date = "2021-11-06 21:21:21";
            System.out.println(convStrToStr(date, "yyyy-MM-dd", "yyyyMMdd"));

        }
    }
